package filter;

public class Binarizer implements FilterModule{
	
	private int thresh = 127; 
	
	public boolean exec( Raster image, int cp, int allowance ){
		int n_did = 0;
		
		int i = cp / image.height + 1;
		int j = cp % image.height + 1;
		
		
		for( ; i < image.height + 1; i++ ){
			for( ; j < image.width + 1; j++ ){
				int gray = ( image.r[ i ][ j ] + image.g[ i ][ j ] + image.b[ i ][ j ] ) / 3;
				int bw = gray < thresh ? 0 : 255;  
				
				image.r[ i ][ j ] = ( short )bw;
				image.g[ i ][ j ] = ( short )bw;
				image.b[ i ][ j ] = ( short )bw;
				
				if( ++cp >= image.height * image.width )
					return true;
				
				if( ++n_did >= allowance )
					return false;
			}
			j = 1;
		}
		
		return true;
	}//end-of-exec()
	
	public String get_name(){
		return "binarize";
	}
}//end-of-class
